import { useSnippetsStore } from '@renderer/stores'

export function useSearchApps () {
  const setSnippets = useSnippetsStore(state => state.setSnippets)

  const searchApps = async (keyword: string) => {
    try {
      const apps = await window.api.searchInstalledApps(keyword)
      
      setSnippets(
        apps.map((app, index) => ({
          id: `app-${index}-${app.name}`,
          title: app.mainFilePath || '未找到可执行文件',
          content: app.name,
          category_id: -1,
          created_at: app.installDate || '',
          url: app.mainFilePath || undefined,
          icon: app.icon, // 直接使用已缓存的本地图标路径
        })),
      )
    } catch (error) {
      console.error('搜索已安装软件失败:', error)
      setSnippets([])
    }
  }

  return {
    searchApps,
  }
}

